<template>
  <el-main>
    <el-button class="list-btn" type="primary" icon="el-icon-plus" @click="createItem" v-if="btnShow">创建</el-button>
    <el-table :data="tableData" :row-class-name="tableRowClassName" v-if="listShow">
      <el-table-column prop="num" label="序号"></el-table-column>
      <el-table-column prop="resourcePath" label="路径名"></el-table-column>
      <el-table-column prop="id" label="操作">
        <template slot-scope="scope">
          <el-button
            class="list-btn"
            type="success"
            @click="editItem(scope.$index,scope.row)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button 
            class="list-btn"
            type="danger"
            @click="deleteItem(scope.$index,scope.row)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-form ref="form" :model="form" label-width="80px" v-if="editShow">
      <el-form-item label="路径名">
        <el-input v-model="form.resourcePath"></el-input>
      </el-form-item>
     
      <el-upload
        class="upload-demo"
        action="/uploadPicture"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :limit="1"
        :on-success="uploadSuccess"
        :on-exceed="handleExceed">
        <el-button size="small" type="primary">点击上传图片</el-button>
      </el-upload>
      
      <el-form-item>
        <el-button type="primary" @click="onSubmit" v-if="newShow==false">确定</el-button>
        <el-button type="primary" @click="add" v-else>确定</el-button>
        <el-button @click="back">取消</el-button>
      </el-form-item>
    </el-form>
  


  </el-main>
</template>

<script type="module">
var pictures = [];
var formData = [];
var editData = [];
var delFlag = false;
var path = "/picture/";
var resourcePath= "";
module.exports = {
  data: function() {
    return {
      tableData: pictures,
      editShow: false,
      listShow: true,
      newShow: false,
      btnShow: true,
      edit: editData,
      emptyForm: {
        id: "",
        resourcePath: ""
      },
      form: {
        id: "",
        resourcePath: ""
      }
    };
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    gettable() {
      $.ajax({
        url: "/listAllPictures",
        type: "get",
        dataType: "json",
        async: false,
        success(data) {
          console.log(data.pictures);
          for (let i in data.pictures) {
            pictures[i] = {
              num: i,
              resourcePath: data.pictures[i].resourcePath,
              id: data.pictures[i].id,
            };
          }
        }
      });
    },
    deleteItem(item, index) {
      let id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/deletePicture",
        type: "post",
        data: JSON.parse(id),
        async: false,
        success(data) {
          if(data) {
            delFlag = true;
          } else {
            delFlag = false;
          } 
        }
      });
      if(delFlag) {
        this.$message({
          message: '恭喜你，删除成功',
          type: 'success'
        });
        setTimeout(function(){
          window.location.href = "/index?page=7";
        }, 1500);
      } else {
        this.$message.error('错误窝，删除失败');
      }
    },
    editItem(item, index) {
      this.newShow = false;
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      let id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/editPicture",
        type: "get",
        dataType: "json",
        async: false,
        data: JSON.parse(id),
        success(data) {
          editData = {
            picture: data.picture
          };
          formData = {
            id: data.picture.id,
            resourcePath: data.picture.resourcePath
          };
        }
      });
      this.edit = editData;
      this.form = formData;
    },
    check(objs, obj) {
      for (let i in objs) {
        if (objs[i].id == obj.id) return true;
      }
      return false;
    },
    onSubmit() {
      console.log(JSON.stringify(this.form));
      $.ajax({
        url: "/updatePicture",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=7";
        }
      });
    },
    back() {
      this.listShow = true;
      this.editShow = false;
      this.btnShow = true;
      this.emptyForm = {
        id: "",
        resourcePath: ""
      };
    },
    createItem() {
      this.newShow = true;
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      $.ajax({
        url: "/newPicture",
        type: "get",
        dataType: "json",
        async: false
      });
      this.form = this.emptyForm;
    },
    add() {
      console.log(JSON.stringify(this.form));
      $.ajax({
        url: "/addPicture",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=7";
        }
      });
    },
    handleRemove(file) {
      // 删除文件时的钩子
      resourcePath = "";
    },
    handlePreview(file) {
      // 点击文件列表中已上传的文件时的钩子
    },
    handleExceed(files) {
      // 超出限制的钩子
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    uploadSuccess(response,file,fileList){
      //文件上传成功时的钩子
      resourcePath = path + file.name;
      this.form.resourcePath = resourcePath;
    }
  },
  mounted: function() {
    // console.log("3");
    this.gettable();
    // console.log("4");
  }
};
</script>